﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spider.DB;
using Spider.Rules;
using Spider.Analyse;
using Spider.SpiderTask;
using Spider.Get;
using SpiderEntity.Fiction;
using Spider.HTMLAnalyse.小说.八零电子书;

namespace Spider.Init.小说.八零电子书
{
    public class Init八零列表 : Iinit
    {
        #region 属性
        /// <summary>
        /// HTML页面解析XPath
        /// </summary>
        IRule rule { get; set; }

        /// <summary>
        /// 页面解析规则
        /// </summary>
        IAnalyse analyse { get; set; }

        /// <summary>
        /// 数据库持久化方式
        /// </summary>
        IDBs db { get; set; }

        /// <summary>
        /// 网站HostURL
        /// </summary>
        string hostUrl { get; set; }

        static object o = new object();
        List<string> errorList = new List<string>();
        string cookie { get; set; }

        #endregion

        #region 设置数据库持久化方式
        /// <summary>
        /// 设置数据库持久化方式
        /// </summary>
        /// <param name="db"></param>
        public void SetDB(IDBs db)
        {
            this.db = db;
        }
        #endregion

        #region 启动爬虫
        /// <summary>
        /// 启动爬虫
        /// </summary>
        public void Start()
        {
            analyse = new 八零电子书列表页面解析();
            cookie = "BAIDUID~C2122BAA2EBAD6DCA6C7350524151B7E:FG=1~.baidu.com%^&BDORZ~B490B5EBF6F3CD402E515D22BCDA1598~.baidu.com%^&BIDUPSID~166299973F4A90956CC600914EA58FCF~.baidu.com%^&CNZZDATA5092477~cnzz_eid%3D313730257-1538439962-%26ntime%3D1538468113~www.44txt.com%^&HMACCOUNT~D7176DC8B8A403D6~.hm.baidu.com%^&HMVT~6bcd52f51e9b3dce32bec4a3997715ac|1538471713|64ecd82404c51e03dc91cb9e8c025574|1538469941|~.hm.baidu.com%^&H_PS_PSSID~1427_21103_26350~.baidu.com%^&Hm_lpvt_a7f940797b9b4505c031a25ee8c1291b~1538473227~.44txt.com%^&Hm_lvt_a7f940797b9b4505c031a25ee8c1291b~1538442114~.44txt.com%^&PSINO~1~.baidu.com%^&PSTM~1538442445~.baidu.com%^&UM_distinctid~166324bb55c883-09777ce2500fd-8383268-1fa400-166324bb55eba0~.44txt.com%^&atpsida~1d501b118bd638de030f5345_1538473226_3~.cnzz.mmstat.com%^&cna~jcg5FO8RjikCAd9IK2aSgZGs~.mmstat.com%^&delPer~0~.baidu.com%^&sca~0f592f56~.cnzz.mmstat.com";
            #region URL 列表
            hostUrl = "http://www.44txt.com";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("玄幻魔法", "http://www.44txt.com/read/xuanhuanmofa/");
            dic.Add("武侠修真", "http://www.44txt.com/read/wuxiaxiuzhen/");
            dic.Add("现代都市", "http://www.44txt.com/read/xiandaidushi/");
            dic.Add("言情小说", "http://www.44txt.com/read/yanqingxiaoshuo/");
            dic.Add("历史军事", "http://www.44txt.com/read/lishijunshi/");
            dic.Add("游戏竞技", "http://www.44txt.com/read/youxijingji/");
            dic.Add("科幻灵异", "http://www.44txt.com/read/kehuanlingyi/");
            dic.Add("耽美小说", "http://www.44txt.com/read/danmeixiaoshuo/");
            dic.Add("同人小说", "http://www.44txt.com/read/tongrenxiaoshuo/");
            dic.Add("其他类型", "http://www.44txt.com/read/qitaleixing/");
            dic.Add("仙侠", "http://www.44txt.com/read/xianxia/");
            dic.Add("科幻", "http://www.44txt.com/read/kehuan/");
            dic.Add("惊悚", "http://www.44txt.com/read/jing/");
            dic.Add("奇幻", "http://www.44txt.com/read/qihuan/");
            dic.Add("异界", "http://www.44txt.com/read/yijie/");
            dic.Add("神话", "http://www.44txt.com/read/shenhua/");
            dic.Add("魔法", "http://www.44txt.com/read/mofa/");
            dic.Add("玄幻", "http://www.44txt.com/read/xuanhuan/");
            dic.Add("历史", "http://www.44txt.com/read/lishi/");
            dic.Add("武侠", "http://www.44txt.com/read/wuxia/");
            dic.Add("游戏", "http://www.44txt.com/read/youxi/");
            dic.Add("谍战", "http://www.44txt.com/read/diezhan/");
            dic.Add("军事", "http://www.44txt.com/read/junshi/");
            dic.Add("竞技", "http://www.44txt.com/read/jingji/");
            dic.Add("官场", "http://www.44txt.com/read/guanchang/");
            dic.Add("言情", "http://www.44txt.com/read/yanqing/");
            dic.Add("校园", "http://www.44txt.com/read/xiaoyuan/");
            dic.Add("宫廷", "http://www.44txt.com/read/gongting/");
            dic.Add("耽美", "http://www.44txt.com/read/danmei/");
            dic.Add("都市", "http://www.44txt.com/read/dushi/");
            dic.Add("推理", "http://www.44txt.com/read/zhentantuili/");
            dic.Add("穿越", "http://www.44txt.com/read/chuanyue/");
            dic.Add("同人", "http://www.44txt.com/read/tongren/");
            dic.Add("短篇", "http://www.44txt.com/read/duanpian/");
            #endregion
            var dicfirst = dic.FirstOrDefault();
            var test = 八零Task.TaskNew(() => SpiderHTML(dicfirst.Key, dicfirst.Value));
            test.Start();

            test.Wait();
            foreach (var item in dic)
            {
                if (false)
                {
                    八零Task.TaskNew(() => SpiderHTML(item.Key, item.Value));
                }
            }
        }
        #endregion

        #region 页面抓取
        /// <summary>
        /// 抓取HTML页面
        /// </summary>
        /// <param name="fenlei"></param>
        /// <param name="url"></param>
        private void SpiderHTML(string fenlei, string url)
        {
            List<string> listUrl = new List<string>();
            //获取到列表的url
            string html = GetHTML.Get(url, cookie: cookie);
            if (string.IsNullOrEmpty(html))
            {
                AddList(url);
            }
            else
            {
                //保存HTML页面
                save(url, html, fenlei);
                ///解析出最后一页的URL地址
                ///从当前页面开始遍历，直接遍历到最后一页
                ///保存当前HTML页面
                string tempUrl = analyse.AnalyseHtml(html, "/html/body/div[4]/div[2]/div/div[2]");
                SpiderHTML(fenlei, hostUrl + tempUrl);
            }
        }
        #endregion

        #region 保存列表页面
        private void save(string url, string html, string fenlei)
        {
            listHTML entity = new listHTML()
            {
                Url = url,
                Html = html,
                FenLei = fenlei,
                Source = "八零电子书"
            };
            db.Save<listHTML>(entity);
        }
        #endregion

        #region 辅助方法
        private List<string> GetList()
        {
            return errorList;
        }

        public void AddList(string str)
        {
            lock (o)
            {
                errorList.Add(str);
            }
        }
        #endregion

    }
}
